/**
 * 
 */
package com.hsnr.findyourbuddy;

import java.io.IOException;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class SettingsActivity extends PreferenceActivity {

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }

    /* (non-Javadoc)
     * @see android.preference.PreferenceActivity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        final String mobile = sharedPrefs.getString("prefMobile", "");
        final String ip = sharedPrefs.getString("prefIp", "");
        final String port = sharedPrefs.getString("prefPort", "");
        
        
        if (!mobile.equals("") && !ip.equals("") && !port.equals("")) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                        try {
                            
                            CommunicationHandler ch = new CommunicationHandler(ip, Integer.parseInt(port));
                            ch.sendSingleRequest(getApplicationContext(), mobile);
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }).start();
        }
        super.onDestroy();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onBackPressed()
     */
    @Override
    public void onBackPressed() {
        final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        final String mobile = sharedPrefs.getString("prefMobile", "");
        if(!mobile.equals("")) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "Bitte geben Sie eine Mobilfunknummer ein!", Toast.LENGTH_SHORT).show();
        }
    }

    
}
